package com.example.managemyself.util;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.lifecycle.LiveData;

import com.example.managemyself.ManageMyself;
import com.example.managemyself.data.database.AppDatabase;
import com.example.managemyself.model.UserSettings;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 设置管理工具类，提供用户设置的访问和修改功能
 */
public class SettingsManager {
    
    private static final String PREF_NAME = "settings_prefs";
    private static final String KEY_CURRENT_THEME = "current_theme";
    private static final String KEY_NOTIFICATIONS_ENABLED = "notifications_enabled";
    
    // 单例实例
    private static SettingsManager instance;
    
    // 线程池
    private final ExecutorService executorService;
    
    // 私有构造函数
    private SettingsManager() {
        executorService = Executors.newSingleThreadExecutor();
    }
    
    /**
     * 获取单例实例
     */
    public static synchronized SettingsManager getInstance() {
        if (instance == null) {
            instance = new SettingsManager();
        }
        return instance;
    }
    
    /**
     * 获取用户设置
     */
    public LiveData<UserSettings> getUserSettings(int userId) {
        AppDatabase database = AppDatabase.getInstance(ManageMyself.getAppContext());
        return database.userSettingsDao().getUserSettingsByUserId(userId);
    }
    
    /**
     * 同步获取用户设置
     */
    public UserSettings getUserSettingsSync(int userId) {
        AppDatabase database = AppDatabase.getInstance(ManageMyself.getAppContext());
        UserSettings settings = database.userSettingsDao().getUserSettingsByUserIdSync(userId);
        
        // 如果没有设置记录，创建一个新的
        if (settings == null) {
            settings = new UserSettings(userId);
            saveUserSettings(settings);
        }
        
        return settings;
    }
    
    /**
     * 保存用户设置
     */
    public void saveUserSettings(UserSettings settings) {
        executorService.execute(() -> {
            AppDatabase database = AppDatabase.getInstance(ManageMyself.getAppContext());
            database.userSettingsDao().insertUserSettings(settings);
        });
    }
    
    /**
     * 更新用户设置
     */
    public void updateUserSettings(UserSettings settings) {
        executorService.execute(() -> {
            AppDatabase database = AppDatabase.getInstance(ManageMyself.getAppContext());
            database.userSettingsDao().updateUserSettings(settings);
        });
    }
    
    /**
     * 获取当前主题
     */
    public String getCurrentTheme(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getString(KEY_CURRENT_THEME, ThemeHelper.THEME_LIGHT);
    }
    
    /**
     * 设置当前主题
     */
    public void setCurrentTheme(Context context, String theme) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(KEY_CURRENT_THEME, theme).apply();
    }
    
    /**
     * 检查是否为夜间模式
     */
    public boolean isNightMode(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean("night_mode", false);
    }
    
    /**
     * 设置夜间模式
     */
    public void setNightMode(Context context, boolean isNightMode) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean("night_mode", isNightMode).apply();
    }
    
    /**
     * 检查通知是否启用
     */
    public boolean isNotificationsEnabled(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        return prefs.getBoolean(KEY_NOTIFICATIONS_ENABLED, true);
    }
    
    /**
     * 设置通知启用状态
     */
    public void setNotificationsEnabled(Context context, boolean enabled) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(KEY_NOTIFICATIONS_ENABLED, enabled).apply();
    }
    
    /**
     * 应用主题
     */
    public void applyTheme(String theme) {
        ThemeHelper.applyTheme(theme);
    }
} 